package com.distribute.application.api.rest.delivery;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.model.BindingDistributor;
import com.distribute.framework.model.Distributor;
import com.distribute.framework.model.Member;
@RestController(value="api.DeliveryAccountController")
public class DeliveryAccountController extends BaseApiController{
	/**
	 * 检测是否是配送商
	 * @return
	 */
	@RequestMapping("/api/delivery/isDelivery.do")
	public @title("检测是否是配送商") Protocol isDelivery(
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=Distributor.where.member(member.getId()).get();
		if (distributor==null) {
			protocol.set("type", 3);
			protocol.set("msg", "未注册");
			return protocol;
		}
		List<BindingDistributor> bindingDistributors=BindingDistributor.where.distributor(distributor.getId()).list(2);
		if (bindingDistributors.size()>0) {
			protocol.set("type", 1);
			protocol.set("msg", "直接登录");
			return protocol;
		}else {
			protocol.set("type", 2);
			protocol.set("msg", "等待品牌商绑定您");
			return protocol;
		}
	}
	
	/**
	 * 配送商检测手机号码
	 * @return
	 */
	@RequestMapping("/api/delivery/isPhoneExsit.do")
	public @title("配送商检测手机号码") Protocol isPhoneExsit(
			@title("手机号") String mobile
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=Distributor.where.mobile(mobile).get();
		if (distributor==null) {
			protocol.set("type", 1);
			protocol.set("msg", "未被注册");
			return protocol;
		}else {
			protocol.set("type", 2);
			protocol.set("msg", "手机号已被注册");
			return protocol;
		}
	}
	/**
	 * 配送商注册接口
	 * @return
	 */
	@RequestMapping("/api/delivery/deliveryRegister.do")
	public @title("配送商注册接口") Protocol deliveryRegister(
			@title("手机号") String mobile,
			@title("formId") String formId,
			@title("密码") String password,
			@title("验证码") String code,
			@title("纬度") String latitude,
			@title("经度") String longitude,
			@title("坐标地址") String coordinate
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=new Distributor(
				member.getNickName()//配送商名称 [非空]
			 	,""//店面图片
			 	,mobile//手机号 [非空]
			 	,""
			 	,Utils.md5(password)
			 	,""//配送商地址
			 	,null
			 	,member.getId()
			 	,""//提货时间
			 	,longitude//经度
	 	        ,latitude//纬度
			 	,(short)2//不同意
	 	        ,(short)1//未完善
			 	,coordinate//坐标地址
			 	,""//审核意见
			 	,formId//formId
			 	,new Date()//修改时间 [非空]
			 	,new Date()//添加时间 [非空]
			 	,(short)0//状态 [非空]
				);
		distributor.save();
		protocol.set("type", 1);
		protocol.set("msg", "注册成功");
		return protocol;
	}
	/**
	 * 配送商修改登录密码
	 * @return
	 */
	@RequestMapping("/api/delivery/changePasswords.do")
	public @title("配送商修改登录密码") Protocol changePasswords(
			@title("原密码") String ypassword,
			@title("新密码") String passworda
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=Distributor.where.member(member.getId()).get();
		if (!distributor.getPassword().equals(Utils.md5(ypassword))) {
			protocol.set("msg", "原密码错误");
			protocol.set("type", 2);
			return protocol;
		}else {
			distributor.setPassword(Utils.md5(passworda));
			distributor.update();
			protocol.set("type", 1);
			protocol.set("msg", "修改成功");
			return protocol;
		}
	}
	/**
	 * 配送商修改基本信息
	 * @return
	 */
	@RequestMapping("/api/delivery/editDistributors.do")
	public @title("配送商修改基本信息") Protocol editDistributors(
			@title("经度") String longitude,
			@title("纬度") String latitude,
			@title("坐标地址") String coordinate,
			@title("所在地址") Long location,
			@title("图片") String aimg,
			@title("店铺名称") String dname,
			@title("联系人") String xname,
			@title("手机号") String mobile,
			@title("详细地址") String adressName,
			@title("提货时间") String tiTime
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=Distributor.where.member(member.getId()).get();
		distributor.setName(dname);
		distributor.setShopPicture(aimg);
		distributor.setMobile(mobile);
		distributor.setContacts(xname);
		distributor.setAddress(adressName);
		distributor.setLocation(location);
		distributor.setDeliveryTime(tiTime);
		distributor.setLongitude(longitude);
		distributor.setLatitude(latitude);
		distributor.setTermsConditions((short)1);
		distributor.setPerfectSituation((short)2);
		distributor.setCoordinateAddress(coordinate);
		distributor.setUpdateTime(new Date());
		distributor.update();
		protocol.set("msg", "修改成功");
		return protocol;
	}
	/**
	 * 配送商登录接口
	 * @return
	 */
	@RequestMapping("/api/delivery/deliveryLogin.do")
	public @title("配送商登录接口") Protocol deliveryLogin(
			@title("手机号") String mobile,
			@title("密码") String password
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
        if (mobile==null||mobile.equals("")) {
			protocol.set("type", 5);
			protocol.set("msg", "手机号不能为空");
			return protocol;
		}
        if (password==null||password.equals("")) {
			protocol.set("type", 4);
			protocol.set("msg", "密码不能为空");
			return protocol;
		}
        Distributor distributor=Distributor.where.mobile(mobile).get();
        if (distributor==null) {
        	protocol.set("type", false);
			protocol.set("msg", "账号不存在");
			return protocol;
		}else {
			if (!distributor.getPassword().equals(Utils.md5(password))) {
				protocol.set("type", false);
				protocol.set("msg", "密码错误");
				return protocol;
			}else {
				if (!distributor.getMember().toString().equals(member.getId().toString())) {
					protocol.set("type", false);
					protocol.set("msg", "不能使用别人的账号进行登录");
					return protocol;
				}else {
					if (distributor.getStatus()==(short)1) {
						protocol.set("type", 1);
						protocol.set("distributor", distributor.toJSON());
						return protocol;
					}else {
						protocol.set("type", false);
						
						if (distributor.getStatus()==(short)2) {
							protocol.set("msg", distributor.getStatus$()+" "+distributor.getAuditOpinion());
						}else {
							protocol.set("msg", distributor.getStatus$());
						}
						
						return protocol;
					}
					
				}
				
			}
		}
	}
}
